package com.qiankun.tang.ui.view.adapter.player;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;

import com.common.library.listener.OnAppListener;
import com.common.library.ui.view.ShadeTextView;
import com.common.library.ui.view.adapter.base.BaseRecyclerAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.PlayerItem;

import static com.qiankun.tang.R.id.item_player_date;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.view.adapter.player
 * @date 2017/6/14 0014
 */

public class PlayerAdapter extends BaseRecyclerAdapter<PlayerItem> {
    public PlayerAdapter(Context context) {
        super(context);
    }

    @Override
    public int getItemViewType(int position) {
        return getDatas().get(position).getType();
    }

    public PlayerAdapter(Context context, OnAppListener.OnInteractionListener listener) {
        super(context, listener);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case PlayerItem.TYPE_0:
                return new PlayerViewHolder0(LayoutInflater.from(getContext()).inflate(R.layout.item_player_0, parent, false));
            case PlayerItem.TYPE_1:
                return new PlayerViewHolder1(LayoutInflater.from(getContext()).inflate(R.layout.item_player_1, parent, false));
            case PlayerItem.TYPE_2:
                return new PlayerViewHolder2(LayoutInflater.from(getContext()).inflate(R.layout.item_player_2, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PlayerViewHolder0) {
            onBindPlayerViewHolder0((PlayerViewHolder0) holder, position);
        } else if (holder instanceof PlayerViewHolder1) {
            onBindPlayerViewHolder1((PlayerViewHolder1) holder, position);
        } else if (holder instanceof PlayerViewHolder2) {
            onBindPlayerViewHolder2((PlayerViewHolder2) holder, position);
        }
    }

    private void onBindPlayerViewHolder2(PlayerViewHolder2 holder, int position) {
        final VideoSynopsisBean bean = (VideoSynopsisBean) getDatas().get(position).getBean();
        holder.mTitle.setText(bean.getTitle());
        holder.mSubTitle.setText(bean.getData());
        holder.mImg.setImageURI(bean.getImgUrl());

        if (bean.getAdmin() == 0) {
            buildObjectAnimator(holder.itemView, 0, 0, 0.0f, 1.0f).setDuration(800).start();
            bean.setAdmin(1);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getListener().onInteraction(PlayerItem.TYPE_2, bean);
            }
        });
    }

    private void onBindPlayerViewHolder1(PlayerViewHolder1 holder, int position) {
        String data = (String) getDatas().get(position).getBean();
        holder.mTitle.setText(data);
    }

    private void onBindPlayerViewHolder0(final PlayerViewHolder0 holder, int position) {
        VideoSynopsisBean bean = (VideoSynopsisBean) getDatas().get(position).getBean();
        holder.mTitle.setText(bean.getTitle());
        holder.mSubTitle.setText(bean.getSubtitle());
        holder.mDate.setText(bean.getData());

        if (bean.getAdmin() == 0) {
            AnimatorSet ser = buildObjectAnimator(holder.itemView, -dip2px(60), 0, 0.5f, 1.0f);
            ser.setDuration(400);
            ser.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    holder.mLine.setVisibility(View.VISIBLE);
                }
            });
            ser.start();
            holder.mTitle.startAnim(600);
            holder.mSubTitle.startAnim(600);
            holder.mDate.startAnim(600);
            bean.setAdmin(1);
        }
    }

    private AnimatorSet buildObjectAnimator(View view, float start, float end, float starta, float ende) {
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, start, end);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(view, View.ALPHA, starta, ende);
        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new AccelerateInterpolator());
        set.playTogether(animator2, animator3);
        return set;
    }

    class PlayerViewHolder0 extends RecyclerView.ViewHolder {
        ShadeTextView mTitle, mSubTitle, mDate;
        View mLine;

        public PlayerViewHolder0(View itemView) {
            super(itemView);
            mLine = itemView.findViewById(R.id.item_player_line);
            mTitle = (ShadeTextView) itemView.findViewById(R.id.item_player_title);
            mDate = (ShadeTextView) itemView.findViewById(item_player_date);
            mSubTitle = (ShadeTextView) itemView.findViewById(R.id.item_player_subTitle);
        }
    }

    class PlayerViewHolder1 extends RecyclerView.ViewHolder {
        TextView mTitle;

        public PlayerViewHolder1(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.item_player_1_title);
        }
    }

    class PlayerViewHolder2 extends RecyclerView.ViewHolder {
        TextView mTitle, mSubTitle;
        SimpleDraweeView mImg;

        public PlayerViewHolder2(View itemView) {
            super(itemView);
            mImg = (SimpleDraweeView) itemView.findViewById(R.id.item_player_2_img);
            mTitle = (TextView) itemView.findViewById(R.id.item_player_2_title);
            mSubTitle = (TextView) itemView.findViewById(R.id.item_player_2_subtitle);
        }
    }
}
